<?php
/*
# TCE - Tight Control Environment: Hosting Platform   
# Copyright (c) 2006 by the TightControl Dev Team
# http://tce.mystichost.net
#
#
# License:
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the MPL Mozilla Public License
#    as published by the Free Software Foundation; either version 1.1    
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
#    MPL Mozilla Public License for more details.
#    
#    You may have received a copy of the MPL Mozilla Public License
#    along with this program.
#    
#    An on-line copy of the MPL Mozilla Public License can be found
#    http://www.mozilla.org/MPL/MPL-1.1.html
#
#
# The TCE Home Page is at:
#
#    http://tce.mystichost.net
*/

// Load the exception classes, if not loaded yet!
require_once 'Exceptions.classes.php'; 

class ConfigResult {
	function __get($Key){
		$Key = strtolower($Key);
		
		if (isset($this->$Key)){
			return $this->$Key;
		}
		return NULL; // Not set, return NULL (this is a value that cannot be returned by the config itself! :)
	}
}
class Config {
	private static $Files = array();
	public function __construct(){
	}
	public function &Factory($Filename, $Reload = false){
		if (isset(Config::$Files[$Filename])){
			if ($Reload){
				Config::$Files[$Filename] = null;
				unset(Config::$Files[$Filename]);				
			}else{
				return Config::$Files[$Filename];
			}
		}
		if (!file_exists($Filename)){
			throw new FileNotFoundException("Could not find configuration file $Filename");				
		}
		
		$Data = @file_get_contents($Filename);
		if ($Data === false){
			throw new FileReadException("Could not open configuration file $Filename!");	
		}
		$Data = str_replace("\r\n", "\r\r",  $Data);
		$Data = str_replace("\r\r", "\n", $Data);
		$Data = str_replace("\r", "\n", $Data);
		
		$Lines = explode("\n", $Data);
		$ConfigResult = new ConfigResult();
		
		for ($i=0;$i<count($Lines);$i++){
			$Line = &$Lines[$i];
			
			/* Do the required stripping */
			$Line = ltrim($Line);
			if (strlen($Line) < 3) {
				// Ignore empty lines
			}else if ($Line[0] == '#' || $Line[0] == ';' || ($Line[0] == '/' && $Line[1] == '/')) {
				// # or ; or // are all comments :)
			}else if (strpos($Line,'=') === false) {
				// No = char found, so no assigning possible
				throw new ConfigInvalidException($Filename, $i + 1);
			}else {
			    $Arr = explode('=',$Line,2);
			
			    $Arr[0] = ltrim($Arr[0]);
			    $Arr[0] = rtrim($Arr[0]);
			
			    $Arr[1] = ltrim($Arr[1]);
			    $Arr[1] = rtrim($Arr[1]);
			    
			    /* Parse $Arr[1] */
			    $Arr[1] = self::ParseValue($Arr[1]);
			
			    /* Feed it into our result */
			    $Arr[0] = strtolower($Arr[0]);
			    $ConfigResult->$Arr[0] = $Arr[1];
			}
		}
		Config::$Files[$Filename] = &$ConfigResult;
		/* return the result */
		return $ConfigResult;
	}
	// Parses stuff like yes, no, 1, 0, true, false, 120, 120.546456, Any Text
	private static function ParseValue($Value){
		$TestValue = strtolower($Value);
		if (($TestValue == 'yes') || ($TestValue == 'on') || ($TestValue == '1') || ($TestValue == 'true')){
			return true;
		}else if (($TestValue == 'no') || ($TestValue == 'off')|| ($TestValue == '0') || ($TestValue == 'false')){
			return false;
		}else if (strval(intval($TestValue)) == $TestValue){
			return intval($TestValue);
		}else if (strval(floatval($TestValue)) == $TestValue){
			return floatval($TestValue);
		}else{
			return $Value;
		}	
	}
}
?>